﻿using System;
using System.Windows;
using Communications.PropertySearch;
using Microsoft.Phone.Controls;
using WP7Contrib.Communications;

namespace Communications
{
    using System.Reactive.Linq;

    public partial class MainPage : PhoneApplicationPage
    {
        private readonly ISearchForProperties _propertyService;
        private readonly IConfiguration _appConfig;

        // Constructor
        public MainPage()
        {
            InitializeComponent();

            _appConfig = new AppConfig();

            var resourceClient = new ResourceClient();
            _propertyService = new PropertySearchServices(resourceClient, _appConfig);
        }

        private void GetRequestClick(object sender, RoutedEventArgs e)
        {
            var propertyCriteria = new PropertyCriteria {PlaceName = "sevenoaks"};

            try
            {
                _propertyService.Search(1, 10, propertyCriteria)
                    .ObserveOnDispatcher()
                    .Subscribe(result =>
                                   {
                                       ResultsOverview.Text = "We have results, it worked successfully...";
                                   },
                               exception =>
                                   {
                                       ResultsOverview.Text = "IT FAILED, message - " + exception.Message;
                                   });
            }
            catch (Exception exception)
            {
                Console.WriteLine(exception);
            }
        }
    }
}